<template>
  <div>
    <!-- 面包屑导肮区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      <el-breadcrumb-item>数据报表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!--2.为echarts准备一个具备大小的Dom -->
      <el-row>
        <!-- <el-col :span="12">
          <div id="main" style="width:600px;height:400px"></div>
        </el-col>-->
        <el-col :span="12">
          <el-upload
            :action="uploadURL"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :headers="headerObj"
            :on-success="handleSuccess"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div class="el-upload__tip" slot="tip">支持绝大多数图片格式，单张图片最大支持5MB</div>
          </el-upload>
        </el-col>
      </el-row>
    </el-card>
    <!-- vueCropper 剪裁图片实现-->
    <el-dialog title="图片剪裁" :visible.sync="dialogVisible" append-to-body>
      <div class="cropper-content">
        <div class="cropper" style="text-align:center">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="true"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :original="option.original"
            :autoCrop="option.autoCrop"
            :fixed="option.fixed"
            :fixedNumber="option.fixedNumber"
            :centerBox="option.centerBox"
            :infoTrue="option.infoTrue"
            :fixedBox="option.fixedBox"
            @realTime="realTime"
          ></vueCropper>
        </div>
        <!-- <div
          class="show-preview"
          :style="{'width': previews.w + 'px', 'height': previews.h + 'px',  'overflow': 'hidden',
    'margin': '5px'}"
        >
          <div :style="previews.div">
            <img :src="option.img" :style="previews.img" />
          </div>
          
        </div>-->
        <div style="margin-top:10px">
          <el-button-group>
            <el-button type="primary" icon="el-icon-arrow-left" @click="leftRotate">左旋转</el-button>
            <el-button type="primary" @click="rightRotate">
              右旋转
              <i class="el-icon-arrow-right el-icon--right"></i>
            </el-button>
          </el-button-group>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="finish" :loading="loading">确认</el-button>
      </div>
    </el-dialog>
    <el-dialog title="图片预览" :visible.sync="previewVisible" width="30%">
      <img :src="previewPath" alt class="previewImg" />
    </el-dialog>

    <div style="width:30%">
      <img :src="testUrl" alt class="previewImg" />
    </div>
  </div>
</template>
<script>
//1.导入echarts
import echarts from "echarts";
import _ from "lodash";
export default {
  data() {
    return {
      testUrl: "",
      dialogVisible: false,
      //上传图片的URL地址
      uploadURL: "http://127.0.0.1:8888/api/private/v1/upload",
      //图片上传组件的请求头
      headerObj: {
        Authorization: window.sessionStorage.getItem("token")
      },
      addForm: {
        //图片列表数组
        pics: []
      },
      previewPath: "",
      previewVisible: false,
      //   fileList: [
      //   ],
      // 裁剪组件的基础配置option
      previews: {},
      option: {
        img: "", // 裁剪图片的地址
        info: true, // 裁剪框的大小信息
        outputSize: 1, // 裁剪生成图片的质量
        outputType: "jpeg", // 裁剪生成图片的格式
        canScale: true, // 图片是否允许滚轮缩放
        autoCrop: true, // 是否默认生成截图框
        autoCropWidth: 100, // 默认生成截图框宽度
        autoCropHeight: 200, // 默认生成截图框高度
        fixedBox: false, // 固定截图框大小 不允许改变
        fixed: true, // 是否开启截图框宽高固定比例
        fixedNumber: [4, 3], // 截图框的宽高比例
        full: true, // 是否输出原图比例的截图
        canMoveBox: true, // 截图框能否拖动
        original: false, // 上传图片按照原始比例渲染
        centerBox: false, // 截图框是否被限制在图片里面
        infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
      },
      //   picsList: [], //页面显示的数组
      // 防止重复提交
      loading: false
    };
  },
  created() {},
  methods: {
    //处理移除图片的操作
    handleRemove(file) {
      //获取将要删除的图片的临时路径
      const filePath = file.response.data.tmp_path;
      // 从pics数组中，找到这个图片对应的索引值
      const i = this.addForm.pics.findIndex(item => item.pic == filePath);
      // 调用数组的splice方法。把图片信息对象从pics数组中移除
      this.addForm.pics.splice(i, 1);
      console.log(this.addForm);
    },
    //监听图片上传成功的事件
    handleSuccess(response) {
      console.log(response);
      this.$nextTick(() => {
        this.option.img = response.data.url;
        this.testUrl = response.data.url;
        console.log(this.option.img);
        this.dialogVisible = true;
      });
      //1.拼接得到的一个图片信息对象
      const picInfo = { pic: response.data.tmp_path };
      //2.将图片信息对象push到PICS数组中
      this.addForm.pics.push(picInfo);
    },
    leftRotate() {
      this.$refs.cropper.rotateLeft(); //向左边旋转90度
    },
    rightRotate() {
      this.$refs.cropper.rotateRight(); //向左边旋转90度
    },
    //处理图片预览
    handlePreview(file) {
      this.previewPath = file.response.data.url;
      this.previewVisible = true;
    },
    finish() {
      this.$refs.cropper.getCropData(data => {
        console.log(data);
        this.testUrl = data;
        this.loading = false;
        this.dialogVisible = false;
      });
      this.$refs.cropper.getCropBlob(data => {
        console.log(data);
        this.testUrl = window.URL.createObjectURL(data);
        this.loading = false;
        this.dialogVisible = false;
      });
    },
    realTime(data) {
      var previews = data;
      var h = 0.5;
      var w = 0.2;

      this.previewStyle1 = {
        width: previews.w + "px",
        height: previews.h + "px",
        overflow: "hidden",
        margin: "0",
        zoom: h
      };

      this.previewStyle2 = {
        width: previews.w + "px",
        height: previews.h + "px",
        overflow: "hidden",
        margin: "0",
        zoom: w
      };
      this.previews = data;
    }
  },
  // 此时页面上的元素已经被渲染完毕了
  async mounted() {}
};
</script>
<style lang="less" scoped>
// 截图
.cropper-content {
  .cropper {
    width: auto;
    height: 300px;
  }
}
.previewImg {
  width: 100%;
}
.show-preview {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
</style>       